<?php
/**
 * Class that operate on table 'rentalsdetail'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2013-05-22 23:18
 */
class RentalsdetailMySqlDAO implements RentalsdetailDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return RentalsdetailMySql 
	 */
	public function load($id){
		$sql = 'SELECT * FROM rentalsdetail WHERE PKRentalsDetail = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM rentalsdetail';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM rentalsdetail ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param rentalsdetail primary key
 	 */
	public function delete($PKRentalsDetail){
		$sql = 'DELETE FROM rentalsdetail WHERE PKRentalsDetail = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($PKRentalsDetail);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param RentalsdetailMySql rentalsdetail
 	 */
	public function insert($rentalsdetail){
		$sql = 'INSERT INTO rentalsdetail (FKRentals, FKMovie, Comment, Qauntity, Cost) VALUES (?, ?, ?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->setNumber($rentalsdetail->fKRentals);
		$sqlQuery->setNumber($rentalsdetail->fKMovie);
		$sqlQuery->set($rentalsdetail->comment);
		$sqlQuery->set($rentalsdetail->qauntity);
		$sqlQuery->set($rentalsdetail->cost);

		$id = $this->executeInsert($sqlQuery);	
		$rentalsdetail->pKRentalsDetail = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param RentalsdetailMySql rentalsdetail
 	 */
	public function update($rentalsdetail){
		$sql = 'UPDATE rentalsdetail SET FKRentals = ?, FKMovie = ?, Comment = ?, Qauntity = ?, Cost = ? WHERE PKRentalsDetail = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->setNumber($rentalsdetail->fKRentals);
		$sqlQuery->setNumber($rentalsdetail->fKMovie);
		$sqlQuery->set($rentalsdetail->comment);
		$sqlQuery->set($rentalsdetail->qauntity);
		$sqlQuery->set($rentalsdetail->cost);

		$sqlQuery->setNumber($rentalsdetail->pKRentalsDetail);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM rentalsdetail';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByFKRentals($value){
		$sql = 'SELECT * FROM rentalsdetail WHERE FKRentals = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByFKMovie($value){
		$sql = 'SELECT * FROM rentalsdetail WHERE FKMovie = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}

	public function queryByComment($value){
		$sql = 'SELECT * FROM rentalsdetail WHERE Comment = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByQauntity($value){
		$sql = 'SELECT * FROM rentalsdetail WHERE Qauntity = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByCost($value){
		$sql = 'SELECT * FROM rentalsdetail WHERE Cost = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByFKRentals($value){
		$sql = 'DELETE FROM rentalsdetail WHERE FKRentals = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByFKMovie($value){
		$sql = 'DELETE FROM rentalsdetail WHERE FKMovie = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByComment($value){
		$sql = 'DELETE FROM rentalsdetail WHERE Comment = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByQauntity($value){
		$sql = 'DELETE FROM rentalsdetail WHERE Qauntity = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByCost($value){
		$sql = 'DELETE FROM rentalsdetail WHERE Cost = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return RentalsdetailMySql 
	 */
	protected function readRow($row){
		$rentalsdetail = new Rentalsdetail();
		
		$rentalsdetail->pKRentalsDetail = $row['PKRentalsDetail'];
		$rentalsdetail->fKRentals = $row['FKRentals'];
		$rentalsdetail->fKMovie = $row['FKMovie'];
		$rentalsdetail->comment = $row['Comment'];
		$rentalsdetail->qauntity = $row['Qauntity'];
		$rentalsdetail->cost = $row['Cost'];

		return $rentalsdetail;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return RentalsdetailMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>